package com.kvsl.wechatmonitor.repository;

import com.kvsl.wechatmonitor.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

@Repository
public class MessageRepository {

    private MongoTemplate mongoTemplate;

    @Autowired
    public MessageRepository(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    public List<Message> getMessageBySender(String sender) {
        Query query = Query.query(Criteria.where("sender").is(sender));
        return mongoTemplate.find(query, Message.class);
    }

    public List<Message> getRecentMessage(LocalDateTime time) {
        Query query = Query.query(Criteria.where("send_time").gt(time));
        return mongoTemplate.find(query, Message.class);
    }

}
